package com.hp.ssm.controller;

import com.hp.ssm.bean.Administrator;
import com.hp.ssm.bean.AdministratorExample;
import com.hp.ssm.bean.Doctor;
import com.hp.ssm.bean.DoctorExample;
import com.hp.ssm.service.DoctorService;
import com.hp.ssm.service.UserService;
import com.hp.ssm.util.JsonData;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private DoctorService doctorService;

    /**
     * 获取参数，
     * 进行查询
     * 查询结果判断，是有有值
     * 返回结果
     */
    @RequestMapping("/UserLogin")
    @ResponseBody
    public Object Login(String num, String pwd, String type, String checkCode, HttpSession session) {
        //验证码校验
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNullOrEmpty(checkCode)) {
            map.put("type", "error");
            map.put("msg", "验证码不存在!");
            return map;
        }
        String code = (String) session.getAttribute("code");
        if (!code.equalsIgnoreCase(checkCode)) {
            map.put("type", "error");
            map.put("msg", "验证码不对！");
            return map;
        }
        if (type.equals("0")) {
            AdministratorExample example = new AdministratorExample();
            //登陆校验
            example.createCriteria().andNumEqualTo(num).andPwdEqualTo(pwd);
            Administrator userServices = userService.login(example);
            if (userServices != null) {
                session.setAttribute("admin", userServices);
                map.put("type", "success");
                map.put("msg", "登录成功！");
            } else {
                map.put("type", "error");
                map.put("msg", "登录失败！");

            }
            return map;
        }else{
            DoctorExample example = new DoctorExample();
            //登陆校验
            example.createCriteria().andDnumEqualTo(num).andDpwdEqualTo(pwd);
            Doctor doctor = doctorService.login(example);
            if (doctor != null) {
                session.setAttribute("admin", doctor);
                map.put("type", "success");
                map.put("msg", "登录成功！");
            } else {
                map.put("type", "error");
                map.put("msg", "登录失败！");

            }
            return map;
        }

    }


    @RequestMapping("/index")
    public String index() {
        return "/view/admin/admin.jsp";
    }

    //用户退出
    @RequestMapping("/quit")
    public String quit(HttpSession session) {
        session.removeAttribute("userServices");
        return "/view/admin/login.html";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Administrator administrator,String type,HttpSession session) {
        Map<String,Object> map=new HashMap<>();
        //管理员注册
        if(type.equals("0")){
        int i = userService.add(administrator);
        return  JsonData.buildSuccess(i>0?"管理员添加成功!) ":" 管理员添加失败！");
        }else {
        //医生注册
            Doctor doctor=new Doctor();
            doctor.setDnum(administrator.getNum());
            doctor.setDname(administrator.getName());
            doctor.setDpwd(administrator.getPwd());
            doctor.setDsex(administrator.getSex());
            doctor.setDtel(administrator.getTel());
            doctor.setDaddress(administrator.getAddress());
            int i=doctorService.add(doctor);
            return  JsonData.buildSuccess(i>0?"医生注册成功！":" 医生注册失败！");
        }
        }


}
